﻿$(function() {
    $("table.entry > tbody > tr > td:nth-child(1)").addClass("en_c1");
    $("table.entry > tbody > tr > td:nth-child(2)").addClass("en_c2");
    $("table.entry > tbody > tr > td:nth-child(3)").addClass("en_c3");

    $("td.en_c2 *:text, td.en_c2 *:password, td.en_c2 *:file").addClass("en_input");

    /* list_allow_custom */
    $("div.en_list_allow_custom > select").each(function() {
        if ($(this).find("option:selected").val() == "自定义") {
            $(this).parent().next("div.en_custom").css("display", "block");
        } else {
            $(this).parent().next("div.en_custom").css("display", "none");
        }
    });
    $("div.en_list_allow_custom > select").change(function() {
        if ($(this).find("option:selected").val() == "自定义") {
            $(this).parent().next("div.en_custom").css("display", "block");
        } else {
            $(this).parent().next("div.en_custom").css("display", "none");
            $(this).parents("tr").find(".custom_validator").css("display", "none");
        }
    });
});

function checkcustom(sender, args) {
    var control = document.getElementById(sender.controltovalidate);
    var value = $(control).val();

    if (value.length > 0) {
        args.IsValid = true;
    } else {
        args.IsValid = ($(control).parent().prev("div.en_list_allow_custom").find("select option:selected").val() != "自定义");
    }
}

function checkyear(sender, args) {
    var year_control = document.getElementById(sender.controltovalidate);
    var year = parseInt($(year_control).val());
    if (isNaN(year)) {
        args.IsValid = false;
    } else {
        var today = new Date();
        args.IsValid = (year >= 1900 && year <= today.getFullYear() + 1);
    }
}

function checkauthor(sender, args){

    var author_validator = $(".author_validator")[0];
    var editor_validator = $(".editor_validator")[0];
    var author_control = document.getElementById(author_validator.controltovalidate);
    var editor_control = document.getElementById(editor_validator.controltovalidate);
    var author = $(author_control).val();
    var editor = $(editor_control).val();
    
    if (author.length == 0 && editor.length == 0) {
        $(author_validator).text("作者和编者不能同时为空");
        args.IsValid = false;

        $(editor_validator).text("作者和编者不能同时为空");
        $(editor_validator).css("display", "inline");
    }
}

function checkeditor(sender, args){

    var author_validator = $(".author_validator")[0];
    var editor_validator = $(".editor_validator")[0];
    var author_control = document.getElementById(author_validator.controltovalidate);
    var editor_control = document.getElementById(editor_validator.controltovalidate);
    var author = $(author_control).val();
    var editor = $(editor_control).val();
    
    if (author.length == 0 && editor.length == 0) {
        $(editor_validator).text("作者和编者不能同时为空");
        args.IsValid = false;

        $(author_validator).text("作者和编者不能同时为空");
        $(author_validator).css("display", "inline");
    }
}